<template>
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" style="margin:10px 10px;">
  <el-form-item label="商品名称" prop="name">
    <el-input v-model="ruleForm.name"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="缩略图">
	<table>
		<tr>
			<td><img :src="ruleForm.thumb"  class="doctor-upload-image"></td>
			<td>
				<el-upload
				  class="upload-demo"
				  :on-success="iconSuccess"
				  :show-file-list="false"
				  action="http://www.yiru520.com/admin/index.php/Goods/upload">
				  <el-button size="small" type="primary">点击上传</el-button>
				</el-upload>
			</td>
		</tr>
	</table>
  </el-form-item>
  <el-form-item label="商品分类" prop="name">
    <el-select v-model="ruleForm.goods_categoryid" placeholder="请选择">
		<el-option
		  v-for="item in category"
		  :key="item.catid"
		  :label="item.catname"
		  :value="item.catid">
		</el-option>
    </el-select>
  </el-form-item>
  <el-form-item label="原价" prop="costprice">
    <el-input v-model="ruleForm.costprice"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="现价" prop="currentprice">
    <el-input v-model="ruleForm.currentprice"  style="width:300px;"></el-input>
  </el-form-item>
  <el-form-item label="库存" prop="total">
    <el-input v-model="ruleForm.total"  style="width:300px;"></el-input>
  </el-form-item>
   <el-form-item label="描述" prop="desc">
    <tinymce :height=800 ref="editor" v-model="ruleForm.desc"></tinymce>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</template>
<script>
import Tinymce from '@/components/Tinymce'
  export default {
    components: {Tinymce},
    data() {
	  const validNumCheck = (rule, value, callback) => {
			   
			if (isNaN(value)) {
				callback(new Error('请输入数字'));
			}  else {
				callback();
			}
	  };
      return {
        ruleForm: {
          name: '',
          thumb: '',
		  costprice: 0,
		  currentprice: 0,
		  total: 0,
		  desc: '',
		  sellerid: '',
        },
		category: [],
        rules: {
          name: [
            { required: true, message: '请输入商品名称', trigger: 'blur' },
            { min: 3, max: 25, message: '长度在 3 到 25 个字符', trigger: 'blur' }
          ],
		  total: [
			{ required: true, message: '请输入库存', trigger: 'blur' },
			{ required: true, validator: validNumCheck, trigger: 'blur' }
		  ],
		  costprice: [
			{ required: true, message: '请输入原价', trigger: 'blur' },
			{ required: true, validator: validNumCheck, trigger: 'blur' }
		  ],
		  currentprice: [
			{ required: true, message: '请输入现价', trigger: 'blur' },
			{ required: true, validator: validNumCheck, trigger: 'blur' }
		  ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$axios({
				method:"post",
				url:this.host+"/shop/index.php/Goods/saveGoods",
				data:{
					id : this.$route.query.id,
					name: this.ruleForm.name,
					thumb: this.ruleForm.thumb,
					costprice: this.ruleForm.costprice,
					currentprice: this.ruleForm.currentprice,
					total: this.ruleForm.total,
					desc: this.ruleForm.desc,
					sellerid: this.ruleForm.sellerid,
				}
			}).then((res)=>{
				this.$message({
				  message: '保存成功',
				  type: 'success'
				});
				this.$router.push({ path: 'goods-list' })
			})
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
	  iconSuccess(evnet, file) {
	    if(file.response == 1){
			this.$message({
				  message: '图片已存在',
				  type: 'success'
				});
		}else{
			this.ruleForm.thumb = file.response
		}
	  },
	  getGoods(){
		this.$axios({
			method:"post",
			url:this.host+"/shop/index.php/Goods/getGoods",
			data:{
				id : this.$route.query.id
			}
		}).then((res)=>{
			this.ruleForm = res.data 
		})
	  },
	  getCategory(){
		this.$axios({
			method:"post",
			url:this.host+"/shop/index.php/Goods/getAllCategory",
		}).then((res)=>{
			this.category = res.data
		})
	  },
    },
	mounted () {
		this.getCategory()
		if(this.$route.query.id)
			this.getGoods()	
	}
  }
</script>